package com.yanqu.road.dao.impl.hunt;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.hunt.UserHuntPatronsData;

import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserHuntPatronsDaoImpl extends TempDataObjectDao<UserHuntPatronsData> {

    public Map<Integer, UserHuntPatronsData> getUserHuntPatronsDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_hunt_patrons where userId=?";
        Map<Integer, UserHuntPatronsData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHuntPatronsData data = parse(rs);
                dataMap.put(data.getPatronsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserHuntPatronsData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_hunt_patrons(userId,patronsId,battleTimes,modifyTime) VALUES(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++, data.getBattleTimes());
            call.setLong(i++, data.getModifyTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHuntPatronsData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_hunt_patrons SET battleTimes=?,modifyTime=? WHERE userId=? and patronsId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBattleTimes());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHuntPatronsData parseData(ResultSet rs) throws SQLException {
        UserHuntPatronsData data = new UserHuntPatronsData();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        data.setModifyTime(rs.getLong("modifyTime"));
        return data;
    }
}
